﻿//Copyright (c) 2013 Tilo Szepan, Immo Wache
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
//documentation files (the "Software"), to deal in the Software without restriction, including without limitation
//the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
//and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all copies or substantial portions 
//of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
//CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//DEALINGS IN THE SOFTWARE.


using System;
using System.Drawing;
using System.Windows.Forms;

namespace DuinoClient

{
    public class RenameDialog : Form
    {
//        FormMain _main;
        Button btn_apply;
        TextBox entry;

        static string newName;
        public static string GetGuess(FormMain main, string name)
        {
            newName = null;
            var dlg = new RenameDialog(main, MakeGuess(name));
            dlg.ShowDialog(main);
            return newName;
        }
        public static string MakeGuess(string name)
        {
            if (String.IsNullOrEmpty(name)) return null;
            string path = null;
            int idx = name.LastIndexOf('/');
            if (idx > 0)
            {
                path = name.Substring(0, idx + 1);
                name = name.Substring(idx + 1);
            }
            name = name.Trim('.');
            idx = name.IndexOf('.');
            if (idx > 0)
            {
                string _name = name.Substring(0, (idx > 8 ? 6 : idx)).ToUpper();
                if (idx > 8) _name += "~1";
                string _ext = name.Substring(idx + 1).ToUpper();
                if (_ext.Length > 3) _ext = _ext.Substring(0, 3);
                name = _name + '.' + _ext;
            }
            else
            {
                int l = name.Length;
                name = name.Substring(0, (l > 8 ? 6 : l)).ToUpper();
                if (l > 8) name += "~1";
            }

            return path + name;
        }

        public RenameDialog(FormMain main, string name)
        {
            SuspendLayout();
 //           _main = main;
            Size = new Size(350, 200);
            Controls.Add(Center(name));
            Controls.Add(Buttons());
            Text = "Rename";
            ResumeLayout();
            entry.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
        }

        Control Center(string name)
        {
            var c = new Control();//Panel();
            c.SuspendLayout();
            var l = new Label();
            l.Dock = DockStyle.Fill;
            l.TextAlign = ContentAlignment.MiddleCenter;
            l.Text = "A 8.3 Name is necessary.\nEnter a new Name:";
            entry = new TextBox();
            entry.Width = 150;
            entry.Location = new Point(Size.Width / 2 - 75, Size.Height - 100);
            entry.Text = name;
            entry.KeyPress += new KeyPressEventHandler(entry_KeyPress);
            c.Controls.Add(entry);
            c.Controls.Add(l);
            c.Dock = DockStyle.Fill;
            c.TabStop = false;
            c.ResumeLayout();
            return c;
        }

        void entry_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == (char)Keys.Enter){
                newName = entry.Text;
                Close();
            };
        }
        Control Buttons()
        {
            var btn_close = new Button();
            btn_close.Text = "Cancel";
            btn_close.AutoSize = true;
            btn_close.Dock = DockStyle.Right;
            btn_close.Click += delegate { Close(); };
            btn_apply = new Button();
            btn_apply.Text = "Apply";
            btn_apply.AutoSize = true;
            btn_apply.Dock = DockStyle.Right;
            btn_apply.Click += delegate
            {
                newName = entry.Text;
                Close();
            };

            var c = new Control();
            c.Controls.Add(btn_apply);
            c.Controls.Add(btn_close);
            c.Dock = DockStyle.Bottom;
            c.Height = 30;
            c.TabStop = false;
            return c;
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            Dispose();
        }
    }
}
